package com.paireach.bw.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.bean.DriveOrder;

import java.util.List;

/**
 * Created by ${cqc} on 2017/2/28.
 */

public class FragHomeAdapter extends RecyclerView.Adapter {

    private List<DriveOrder> list;

    public FragHomeAdapter(List<DriveOrder> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_home, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        MyHolder holder = (MyHolder) viewHolder;
        DriveOrder info = list.get(position);
        String transport_code = info.getTRANSPORT_CODE();
        if ("KA".equals(transport_code)) {
            holder.tv1.setText("KA");
        } else if ("BACK".equals(transport_code)) {
            holder.tv1.setText("回空");
        } else {//if ("COMMON".equals(transport_code))
            holder.tv1.setText("常规");
        }
        holder.tv2.setText("负载单号：" + info.getCODE());
        holder.tv3.setText("收货地址：" + info.getDC_ADDRESS());

        ScaleAnimation scale = new ScaleAnimation(0.8f, 1.0f, 0.8f, 1.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scale.setDuration(500);
//        holder.itemView.startAnimation(scale);
        holder.itemView.setAnimation(scale);
        scale.start();

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(view, position);
                }
            });
        }
    }

    /**
     * 使用补间动画+recyclerView快速滑动， item会堆在一起，造成卡顿现象。
     */
    @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

    }

    private OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        TextView tv1;
        TextView tv2;
        TextView tv3;

        public MyHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.tv1);
            tv2 = (TextView) itemView.findViewById(R.id.tv2);
            tv3 = (TextView) itemView.findViewById(R.id.tv3);
        }
    }
}
